package com.example.publishDemo.Mapper;

import com.example.publishDemo.Model.Article;
import com.example.publishDemo.Model.ArticleReport;
import com.example.publishDemo.Model.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface AdminMapper {

    @Select("select count(*) from user")
    int totalUser();

    @Select("select count(*) from articles")
    int totalArticle();

    @Select("select * from articles order by a_id asc limit #{start},#{end} ")
    List<Article> articleRange(int start, int end);

    @Delete("delete from articles where a_id = #{a_id}")
    int delArticleForId(int a_id);

    @Select("select * from user order by u_id asc limit #{start},#{end} ")
    List<User> UserRange(int start, int end);

    @Delete("delete from user where u_id = #{u_id}")
    int delUserForId(int u_id);

    @Select("select count(*) from article_report ")
    int totalReport();

    @Select("select * from article_report order by r_id asc limit #{start},#{end} ")
    List<ArticleReport> ReportRange(int start, int end);

    @Select("select * from user where u_id = #{u_id}")
    User getUserById(int u_id);

    @Select("select * from articles where a_id = #{a_id}")
    Article getArticleForId(int a_id);

    @Update("update article_report set process = 1 where r_id = #{r_id}")
    int completeProcess(int r_id);

    @Select("select count(*) from comment ")
    int totalComment();

    @Select("select sum(views) from articles")
    int totalView();

    @Select("select views from articles order by views desc limit 0,5")
    List<Integer> getArticleTop5Views();

    @Select("select title from articles order by views desc limit 0,5")
    List<String> getArticleTop5Title();

    @Select("select name from tag")
    List<String> getAllTagName();

    @Select("select count(*) from articles where tags like concat('%',#{tagName},'%')")
    int findArticleForTagName(String tagName);

    @Select("select count(*) from articles where a_id = #{a_id} and isRecommend = 0")
    int isRecommend(int a_id);

    @Update("update articles set isRecommend = 1 where a_id = #{a_id}")
    int unRecommend(int a_id);

    @Update("update articles set isRecommend = 0 where a_id = #{a_id}")
    int Recommend(int a_id);

    @Select("select count(*) from user where u_id = #{u_id} and isAdmin = 0")
    int isAdmin(int u_id);

    @Update("update user set isAdmin = 1 where u_id = #{u_id}")
    int unAdmin(int u_id);

    @Update("update user set isAdmin = 0 where u_id = #{u_id}")
    int Admin(int u_id);
}
